package com.george.minio.exception;


import com.george.minio.dto.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import javax.servlet.http.HttpServletRequest;

/**
 * @title: com.george.minio.exception.GlobalExceptionHandler.java
 * @description: 全局异常处理器
 * @author: George
 * @date: 2023/5/16 22:27
 */
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler({Exception.class})
    @ResponseBody
    public Result handleException(HttpServletRequest request, Exception e) throws Exception {
        log.error(e.getMessage(), e);
        // 如果某个自定义异常有@ResponseStatus注解，就继续抛出
        if (AnnotationUtils.findAnnotation(e.getClass(), ResponseStatus.class) != null) {
            throw e;
        }
        return Result.failResult(e.getMessage());
    }
}

